package com.tender.service.impl;

import cn.hutool.core.lang.Assert;
import com.tender.domain.ShiroRoleResource;
import com.tender.domain.ShiroRoleResourceCondition;
import com.tender.domain.ShiroRoleResourceExample;
import com.tender.service.ShiroRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tender.utils.DataUtils;

import java.util.ArrayList;
import java.util.List;

import com.tender.mapper.ShiroRoleResourceMapper;
import org.springframework.util.CollectionUtils;

@Service
public class ShiroRoleResourceServiceImpl implements ShiroRoleResourceService {

    @Autowired
    private ShiroRoleResourceMapper shiroRoleResourceMapper;

    @Override
    public ShiroRoleResource selectOne(ShiroRoleResourceCondition param) {
        Assert.notNull(param.getRoleRowId(), "角色rowId不能为空");
        Assert.notNull(param.getResourceRowId(), "资源rowId不能为空");

        ShiroRoleResourceExample example = new ShiroRoleResourceExample();
        ShiroRoleResourceExample.Criteria criteria = example.createCriteria();

        DataUtils.setLongValueIfNotNull(param.getRoleRowId(), criteria::andRoleRowIdEqualTo);
        DataUtils.setLongValueIfNotNull(param.getResourceRowId(), criteria::andResourceRowIdEqualTo);

        List<ShiroRoleResource> shiroRoleResources = shiroRoleResourceMapper.selectByExample(example);

        return CollectionUtils.isEmpty(shiroRoleResources) ? null : shiroRoleResources.get(0);
    }

    @Override
    public List<ShiroRoleResource> selectList(ShiroRoleResourceCondition param) {
        ShiroRoleResourceExample example = new ShiroRoleResourceExample();
        ShiroRoleResourceExample.Criteria criteria = example.createCriteria();

        DataUtils.setListIfNotEmpty(param.getResourceRowIdList(), criteria::andResourceRowIdIn);
        DataUtils.setListIfNotEmpty(param.getRoleRowIdList(), criteria::andRoleRowIdIn);

        List<ShiroRoleResource> shiroRoleResources = shiroRoleResourceMapper.selectByExample(example);

        return CollectionUtils.isEmpty(shiroRoleResources) ? new ArrayList<>() : shiroRoleResources;
    }

    @Override
    public int updateByCondition(ShiroRoleResourceCondition param) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ShiroRoleResourceCondition param) {
        return 0;
    }

    @Override
    public int insertSelective(ShiroRoleResource param) {
        return 0;
    }

    @Override
    public int insertListSelective(List<ShiroRoleResource> params) {
        return 0;
    }

    @Override
    public int deleteByCondition(ShiroRoleResourceCondition param) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long rowId) {
        Assert.notNull(rowId, "角色资源rowId不能为空");
        ShiroRoleResourceExample example = new ShiroRoleResourceExample();
        ShiroRoleResourceExample.Criteria criteria = example.createCriteria();
        criteria.andRowIdEqualTo(rowId);

        return shiroRoleResourceMapper.deleteByExample(example);
    }
}
